package mortal.game.control
{
    import Message.Public.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.defin.*;
    import mortal.game.mvc.*;
    import mortal.game.scene.layer.utils.*;
    import mortal.game.view.system.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class BattlefieldController extends Controller
    {
        private var _battlefieldModule:BattlefieldModule;
        private var _currentBFState:EBattlefieldPlayerState;
        private var _battlefieldOpen:SBattlefieldOpen;
        private var _isAtStartTime:Boolean = false;
        private var _isInBattlefield:Boolean = false;
        private var lastSettingHideLifeMagic:Boolean = false;
        private var isChangeAtMiddle:Boolean = false;

        public function BattlefieldController()
        {
            this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusClose;
            
        }

        override protected function initView() : IView
        {
            if (!this._battlefieldModule)
            {
                this._battlefieldModule = new BattlefieldModule();
            }
            return this._battlefieldModule;
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldOpen, this.onBattlefieldOpen);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldClose, this.onBattlefieldClose);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldListUpdate, this.onBattlefieldList);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldPlayerScoresUpdate, this.onBattlefieldPlayerScoresUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldPlayerCreditUpdate, this.onBattlefieldPlayerCreditUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldCampCreditUpdate, this.onBattlefieldCampCreditUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldEnter, this.onBattlefieldEnter);
            NetDispatcher.addCmdListener(ServerCommand.BattlefieldLeave, this.onBattlefieldLeave);
            NetDispatcher.addCmdListener(ServerCommand.FightModelCampUnionUpdateCommand, this.onCampUnionUpdate);
            Dispatcher.addEventListener(EventName.BattlefieldShowIntroduce, this.onBattlefieldShowIntroduce);
            Dispatcher.addEventListener(EventName.BattlefieldRequestList, this.onBattlefieldRequestList);
            Dispatcher.addEventListener(EventName.BattlefieldRequestComein, this.onBattlefieldRequestComein);
            Dispatcher.addEventListener(EventName.BattlefieldRequestResult, this.onBattlefieldRequestResult);
            Dispatcher.addEventListener(EventName.BattlefieldRequestLeaveAffirm, this.onBattlefieldRequestLeaveAffirm);
            Dispatcher.addEventListener(EventName.SysSettingSuccess, this.onSettingChange);
            
        }

        private function onBattlefieldOpen(param1:SBattlefieldOpen) : void
        {
            this._battlefieldOpen = param1;
            this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusWait;
            this._isAtStartTime = ClockManager.instance.nowDate.time - this._battlefieldOpen.openDt.time < 5000;
            var _loc_2:* = ClockManager.instance.nowDate.time - this._battlefieldOpen.openDt.time < -2000;
            this.updateViewState();
            if (!this._isAtStartTime && cache.role.entityInfo.level >= 40)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.BattlefieldRequestList));
                (view as BattlefieldModule).showEntrance();
            }
            if (!_loc_2 && cache.role.entityInfo.level >= 40)
            {
                (view as BattlefieldModule).showLightMask();
            }
            else
            {
                (view as BattlefieldModule).hideLightMask();
            }
            if (_loc_2)
            {
                (view as BattlefieldModule).startCountdownLimitEnter(this._battlefieldOpen.openDt);
            }
            
        }

        private function onBattlefieldClose(param1:Object) : void
        {
            if (!this._isInBattlefield)
            {
                this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusClose;
                this.updateViewState();
            }
            (view as BattlefieldModule).setCanLeave();
            
        }

        private function onBattlefieldEnter(param1:Object) : void
        {
            this._isInBattlefield = true;
            this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusOpen;
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyEnterCopyMap));
            this.updateViewState();
            this.battlefieldCountDownUpdate(this._battlefieldOpen.endDt);
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            this.lastSettingHideLifeMagic = SystemSetter.currentSetter.hideLifeMagic;
            SystemSetter.currentSetter.hideLifeMagic = false;
            ThingUtil.entityUtil.updateLifeShow();
            
        }

        private function onBattlefieldLeave(param1:Object) : void
        {
            this._isInBattlefield = false;
            if (this.isInBattlefieldTime())
            {
                this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusWait;
            }
            else
            {
                this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusClose;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveCopyMap));
            this.updateViewState();
            if (this.lastSettingHideLifeMagic && !this.isChangeAtMiddle)
            {
                SystemSetter.currentSetter.hideLifeMagic = true;
                ThingUtil.entityUtil.updateLifeShow();
            }
            
        }

        private function onCampUnionUpdate(param1:SAttribute) : void
        {
            (view as BattlefieldModule).updateUnionState(param1);
            
        }

        private function onSettingChange(event:DataEvent) : void
        {
            this.isChangeAtMiddle = true;
            
        }

        private function battlefieldCountDownUpdate(param1:Date) : void
        {
            (view as BattlefieldModule).startCountdown(param1);
            
        }

        private function onBattlefieldList(param1:Array) : void
        {
            (view as BattlefieldModule).updateBattlefieldList(param1);
            
        }

        private function onBattlefieldPlayerScoresUpdate(param1:Object) : void
        {
            (view as BattlefieldModule).updatePlayerScoreList(cache.battlefield.vccampPlayerScoreAll);
            
        }

        private function onBattlefieldPlayerCreditUpdate(param1:Object) : void
        {
            (view as BattlefieldModule).updateMyScore(cache.battlefield.myScore);
            
        }

        private function onBattlefieldCampCreditUpdate(param1:Object) : void
        {
            (view as BattlefieldModule).updateCampScore(cache.battlefield.campScore1, cache.battlefield.campScore2, cache.battlefield.campScore3);
            
        }

        private function onBattlefieldShowIntroduce(event:DataEvent) : void
        {
            (view as BattlefieldModule).showEntrance();
            
        }

        private function onBattlefieldRequestList(event:DataEvent) : void
        {
            GameProxy.battlefieldProxy.getBattleFieldInfo();
            
        }

        private function onBattlefieldRequestComein(event:DataEvent) : void
        {
            GameProxy.battlefieldProxy.enterBattleField(event.data as int);
            
        }

        private function onBattlefieldRequestResult(event:DataEvent) : void
        {
            GameProxy.battlefieldProxy.getBattleFieldScoreList();
            
        }

        private function onBattlefieldRequestLeaveAffirm(event:DataEvent) : void
        {
            GameProxy.battlefieldProxy.leftBattleField();
            
        }

        private function updateViewState() : void
        {
            (view as BattlefieldModule).updateState(this._currentBFState);
            
        }

        private function isInBattlefieldTime() : Boolean
        {
            if (!this._battlefieldOpen)
            {
                return false;
            }
            if (ClockManager.instance.nowDate.time > this._battlefieldOpen.openDt.time && ClockManager.instance.nowDate.time < this._battlefieldOpen.endDt.time)
            {
                return true;
            }
            return false;
        }

    }
}
